package com.iknight.mgr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.mgr.api.PermGroupService;
import com.iknight.mgr.mapper.PermGroupMapper;
import com.iknight.mgr.mapper.PermissionMapper;
import com.iknight.mgr.model.PermGroupModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by jeryzym on 2017/8/14.
 */
@Service
public class PermGroupServiceImpl implements PermGroupService<PermGroupModel, String> {

    @Autowired
    private PermGroupMapper<PermGroupModel, String> permGroupMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    public void save(PermGroupModel record) {
        if (null == permGroupMapper.selectOne(record.getPerm_group_id())) {
            permGroupMapper.insert(record);
        } else {
            permGroupMapper.update(record);
        }
    }

    @Transactional
    public void delete(String pkey) {
        permissionMapper.deletePermByGroupId(pkey);
        permGroupMapper.delete(pkey);
    }

    public PermGroupModel selectOne(String pkey) {
        return permGroupMapper.selectOne(pkey);
    }

    public PageInfo<PermGroupModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        PageHelper.startPage(pageStart, pageSize, true);
        List<PermGroupModel> result = permGroupMapper.selectByCond(params);
        PageInfo<PermGroupModel> page = new PageInfo<>(result);
        return page;
    }

    public List<PermGroupModel> selectAll() {
        return permGroupMapper.selectAll();
    }
}
